<?php
include($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/Articles/article.php");
include($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/Security/securite.php");
include_once($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/BDD.php");

class catalogue {//création class catalogue
    
    private $bdd;
    private $securite;
    private $articles;
    private $offset;
    
    public function __construct(){//constructeur
        $this->articles = array();
        $this->offset = 0;
        $this->bdd = new BDD();
        $this->securite = new securite();
    }

    public function recupArticles() // Méthode pour visualiser tout le catalogue
    {
        $req = $this->bdd->getBDD()->prepare("SELECT idArticle FROM article LIMIT 10 OFFSET $this->offset");
        $req->execute();
        while($rslt = $req->fetch(PDO::FETCH_ASSOC)){
            $this->articles[] = new article($rslt['idArticle']);
        }
        $this->incremOffset();
    }

    public function addCatalogue() // Méthode pour ajouter article au catalogue
    {
  	
	$req = $this->bdd->getBDD()->prepare("INSERT INTO article (etatarticle, categorie, auteur, editeur, 
	                    article, description, prix, stock, seuilreappro, dateedition, :photo) VALUES (:dispo, :categorie, 
                            :auteur, :editeur, :nomarticle, :description, :prix, :stock, :seuil, :date, :photo)");
						
	$req->execute(array(":dispo"=>$_POST['dispo'], ":categorie"=>$_POST['categorie'], ":auteur"=>$_POST['auteur'],
            ":editeur"=>$_POST['editeur'], ":nomarticle"=>$_POST['nomarticle'], ":description"=>$_POST['description'], 
            ":prix"=>$_POST['prix'],":stock"=>$_POST['stock'], ":seuil"=>$_POST['seuil'], ":date" =>$_POST['date'], ":photo" => $_POST['photo']));
    }

    public function suppCatalogue($idArticle) // Méthode pour supprimer un article du catalogue
    {		
        $req = $this->bdd->getBDD()->prepare("DELETE FROM article WHERE idArticle = $idArticle");
        $req->execute();
    }
  
    public function getArticles(){//getter pour récupérer les articles
        return $this->articles;
    }
    
    private function incremOffset(){
        $this->offset += 10;
    }
    public function rechercher(){
        $req = "SELECT idArticle FROM article WHERE ";
        if(!empty($_POST['nomArticle'])){
            $this->etendreRequete($req);
            $req = $req . "article =".$_POST['nomArticle'];
        }
        
        if(!empty($_POST['categorie'])){
            $this->etendreRequete($req);
            $req = $req . "categorie =".$_POST['categorie'];
        }
        
        if(!empty($_POST['auteur'])){
            $this->etendreRequete($req);
            $req = $req . "auteur =".$_POST['auteur'];
        }
        
        if(!empty($_POST['editeur'])){
            $this->etendreRequete($req);
            $req = $req . "editeur =".$_POST['editeur'];
        }
        
        if(!empty($_POST['prixMin'])){
            $this->etendreRequete($req);
            $req = $req . "prix >".$_POST['prixMin'];
        }
        
        if(!empty($_POST['prixMax'])){
            $this->etendreRequete($req);
            $req = $req . "prix <".$_POST['prixMax'];
        }
        
        $rslt = $this->bdd->getBDD()->prepare($req);
        $rslt->execute();
        while($tab = $rslt->fetch(PDO::FETCH_ASSOC)){
            $this->articles[] = new article($rslt['idArticle']);
        }
    }
  
    private function etendreRequete($req){
        if(strlen($req) > 28){
                $req = $req . " AND ";
        }
        return $req;
    }
}

?>